//
//  GATabBarViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/1/22.
//  Copyright © 2021 热带雨林. All rights reserved.
//

#import "GATabBarViewController.h"
#import "GANavigationViewController.h"
#import <TLAnimationTabBar.h>





@interface GATabBarViewController ()

@end

@implementation GATabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加子控制器方式1
//    UIViewController *homeVC = [self childControllerWithName:@"ViewController" withTitle:@"Pin" withImageName:@"icon_pin_00160"];
//    UIViewController *dropVC = [self childControllerWithName:@"GADropViewController" withTitle:@"Drop" withImageName:@"drop"];
//    UIViewController *userVC = [self childControllerWithName:@"GAUsersViewController" withTitle:@"User" withImageName:@"user_00084"];
//    UIViewController *toolVC = [self childControllerWithName:@"GAToolsViewController" withTitle:@"Tools" withImageName:@"Tools_00028"];
//    UIViewController *bookMarksVC = [self childControllerWithName:@"GABookMarksViewController" withTitle:@"BookMarks" withImageName:@"Settings_00208"];
//    self.viewControllers = @[homeVC,dropVC,bookMarksVC,userVC,toolVC];
    //添加子控制器方式2
    [self addChildViewController:childViewController(@"ViewController", @"Pin", @"icon_pin_00160", 0)];
    [self addChildViewController:childViewController(@"GADropViewController", @"Drop", @"drop", 1)];
    [self addChildViewController:childViewController(@"GABookMarksViewController", @"BookMarks", @"Settings_00208", 2)];
    [self addChildViewController:childViewController(@"GAUsersViewController", @"User", @"user_00084", 3)];
    [self addChildViewController:childViewController(@"GAToolsViewController", @"Tools", @"Tools_00028", 4)];
    
    self.tabBar.tintColor = [UIColor colorWithRed:234/255.0 green:111/255.0 blue:90/255.0 alpha:1];
    if (@available(iOS 13.0, *)) {
        self.tabBar.unselectedItemTintColor = [UIColor colorWithRed:124/255.0 green:99/255.0 blue:86/255.0 alpha:1];
    } else {
        // Fallback on earlier versions
    }
    
}
//创建子控制器方式1
- (UIViewController *)childControllerWithName:(NSString *)name withTitle:(NSString *)title withImageName:(NSString *)imageName{
    Class cla = NSClassFromString(name);
    UIViewController *vc = [[cla alloc]init];
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    GANavigationViewController *naVC = [[GANavigationViewController alloc]initWithRootViewController:vc];
    return naVC;
}
//创建子控制器方法2
UIViewController *childViewController (NSString *name, NSString *title, NSString *imgName, NSUInteger tag) {
    Class cla = NSClassFromString(name);
    UIViewController *vc =  [[cla alloc]init];
    //貌似只有设置背景为白色才能出现动画效果
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:imgName] tag:tag];
    GANavigationViewController *naVC = [[GANavigationViewController alloc]initWithRootViewController:vc];
    setAnimation(vc.tabBarItem, tag);
    return naVC;
}
//UIViewController *childViewController2 (UITabBarSystemItem systemItem, NSUInteger tag) {
//    ViewController *vc = [[ViewController alloc] init];
//    vc.view.backgroundColor = [UIColor whiteColor];
//    vc.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:systemItem tag:tag];
////    setAnimation(vc.tabBarItem, tag);
//    return vc;
//}
//第三方框架TLAnimationTabBar
void setAnimation(UITabBarItem *item, NSInteger index) {
     item.animation = @[
                       bounceAnimation(), rotationAnimation(), transitionAniamtion(),
                       fumeAnimation(), frameAnimation()
                       ][index];
}

TLBounceAnimation *bounceAnimation(){
    TLBounceAnimation *anm = [TLBounceAnimation new];
    return anm;
}

TLRotationAnimation *rotationAnimation(){
    TLRotationAnimation *anm = [TLRotationAnimation new];
    return anm;
}

TLTransitionAniamtion *transitionAniamtion(){
    TLTransitionAniamtion *anm = [TLTransitionAniamtion new];
    anm.direction = 1; // 1~6
    anm.disableDeselectAnimation = NO;
    return anm;
}

TLFumeAnimation *fumeAnimation(){
    TLFumeAnimation *anm = [TLFumeAnimation new];
    return anm;
}

TLFrameAnimation *frameAnimation(){
    TLFrameAnimation *anm = [TLFrameAnimation new];
    anm.images = imgs();
    return anm;
}

NSArray *imgs (){
    NSMutableArray *temp = [NSMutableArray array];
    for (NSInteger i = 28 ; i <= 65; i++) {
        NSString *imgName = [NSString stringWithFormat:@"Tools_000%zi", i];
        CGImageRef img = [UIImage imageNamed:imgName].CGImage;
        [temp addObject:(__bridge id _Nonnull)(img)];
    }
    return temp;
}

//void __sanitizer_cov_trace_pc_guard_init(uint32_t *start,
//                                                    uint32_t *stop) {
//  static uint64_t N;  // Counter for the guards.
//  if (start == stop || *start) return;  // Initialize only once.
//  printf("INIT: %p %p\n", start, stop);
//  for (uint32_t *x = start; x < stop; x++)
//    *x = ++N;  // Guards should start from 1.
//}
//
//void __sanitizer_cov_trace_pc_guard(uint32_t *guard) {
////  if (!*guard) return;
////  void *PC = __builtin_return_address(0);
////  char PcDescr[1024];
////
////  //__sanitizer_symbolize_pc(PC, "%p %F %L", PcDescr, sizeof(PcDescr));
////  printf("guard: %p %x PC %s\n", guard, *guard, PcDescr);
//    //排除load方法
//    //if (!*guard) return;
//
//    //当前函数返回到上一个方法继续执行的地址
//    void *PC = __builtin_return_address(0);
//    Dl_info info;
//    dladdr(PC, &info);
//    printf("fname:%s \nfbase:%p \nsname:%s \nsaddr:%p\n\n\n",info.dli_fname,info.dli_fbase,info.dli_sname,info.dli_saddr);
//
//    char PcDescr[1024];
//    //__sanitizer_symbolize_pc(PC, "%p %F %L", PcDescr, sizeof(PcDescr));
//    printf("guard: %p %x PC %s\n", guard, *guard, PcDescr);
//
//}





@end
